﻿namespace Anmlz.Web.App_Start
{
    using System.Reflection;
    using System.Web.Http;
    using System.Web.Mvc;

    using Anmlz.Data;
    using Anmlz.Data.Contracts;
    using Anmlz.Data.ModelFactory;
    using Anmlz.Data.UnitOfWork;
    using Anmlz.Model.Entities;
    using Anmlz.Services;
    using Anmlz.Services.Contracts;
    using Anmlz.Web.Infrastructure;

    using SimpleInjector;
    using SimpleInjector.Integration.Web.Mvc;

    public static class DependencyResolverInitializer
    {
        public static void Initialize(HttpConfiguration config)
        {
            var container = new Container();

            var services = config.Services;
            var controllerTypes = services.GetHttpControllerTypeResolver()
                .GetControllerTypes(services.GetAssembliesResolver());

            foreach (var controllerType in controllerTypes)
            {
                container.Register(controllerType);
            }

            RegisterServices(container);

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.RegisterMvcAttributeFilterProvider();
            container.Verify();

            config.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);
            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }

        private static void RegisterServices(Container container)
        {

            container.RegisterSingle<IBreedImageTypeService, BreedImageTypeService>();
            container.RegisterSingle<IFileNameService, FileNameService>();
            container.RegisterSingle<IConfigService, ConfigService>();
            container.RegisterSingle<IGeoParser, GeoParser>();
            container.RegisterSingle<ISampleTextGenerator, SampleTextGenerator>();
            container.RegisterSingle<ICryptService, CryptService>();
            container.RegisterSingle<ILanguageService, LanguageService>();
            container.RegisterPerWebRequest<AnmlzDatabaseInitializer>();
            container.RegisterPerWebRequest<AnmlzDbContext>();
            container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
            container.RegisterPerWebRequest<IModelFactory, ModelFactory>();
            container.RegisterPerWebRequest<IRepository<City>, Repository<City>>();
            container.RegisterPerWebRequest<IRepository<User>, Repository<User>>();
            container.RegisterPerWebRequest<IRepository<UserType>, Repository<UserType>>();
            container.RegisterPerWebRequest<IRepository<BreedGroup>, Repository<BreedGroup>>();
            container.RegisterPerWebRequest<IRepository<Breed>, Repository<Breed>>();
            container.RegisterPerWebRequest<IRepository<Country>, Repository<Country>>();
            container.RegisterPerWebRequest<IRepository<Creature>, Repository<Creature>>();
            container.RegisterPerWebRequest<IRepository<CreatureColor>, Repository<CreatureColor>>();
            container.RegisterPerWebRequest<IRepository<CreatureSizeType>, Repository<CreatureSizeType>>();
            container.RegisterPerWebRequest<IRepository<CreatureType>, Repository<CreatureType>>();
            container.RegisterPerWebRequest<IRepository<Currency>, Repository<Currency>>();
            container.RegisterPerWebRequest<IRepository<UserRole>, Repository<UserRole>>();
            container.RegisterPerWebRequest<IRepository<UserWebsiteUrl>, Repository<UserWebsiteUrl>>();
            container.RegisterPerWebRequest<IRepository<Image>, Repository<Image>>();
            container.RegisterPerWebRequest<IRepository<ImageToUser>, Repository<ImageToUser>>();
            container.RegisterPerWebRequest<IRepository<ImageToCreature>, Repository<ImageToCreature>>();
            container.RegisterPerWebRequest<IRepository<SellingAd>, Repository<SellingAd>>();
            container.RegisterPerWebRequest<IRepository<ImageToSellingAd>, Repository<ImageToSellingAd>>();
            container.RegisterPerWebRequest<IRepository<CreatureTemperament>, Repository<CreatureTemperament>>();
            container.RegisterPerWebRequest<IRepository<CreatureCharacteristic>, Repository<CreatureCharacteristic>>();
            container.RegisterPerWebRequest<IRepository<BreedToCreatureTemperament>, Repository<BreedToCreatureTemperament>>();
            container.RegisterPerWebRequest<IRepository<BreedToCreatureColor>, Repository<BreedToCreatureColor>>();
            container.RegisterPerWebRequest<IRepository<BreedToCreatureCharacteristic>, Repository<BreedToCreatureCharacteristic>>();
        }
    }
}